package com.lyh.crm.workbench.web.controller;

import com.lyh.crm.settings.domain.User;
import com.lyh.crm.settings.service.UserService;
import com.lyh.crm.settings.service.impl.UserServiceImpl;
import com.lyh.crm.utils.DateTimeUtil;
import com.lyh.crm.utils.PrintJson;
import com.lyh.crm.utils.ServiceFactory;
import com.lyh.crm.utils.UUIDUtil;
import com.lyh.crm.vo.PaginationVO;
import com.lyh.crm.workbench.domain.*;
import com.lyh.crm.workbench.service.ContactsService;
import com.lyh.crm.workbench.service.CustomerService;
import com.lyh.crm.workbench.service.TranService;
import com.lyh.crm.workbench.service.VisitService;
import com.lyh.crm.workbench.service.impl.ContactsServiceImpl;
import com.lyh.crm.workbench.service.impl.CustomerServiceImpl;
import com.lyh.crm.workbench.service.impl.TranServiceImpl;
import com.lyh.crm.workbench.service.impl.VisitServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VisitController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("进入到售后处理控制器");

        String path = request.getServletPath();

        if ("/workbench/visit/add.do".equals(path)) {

            add(request, response);

        } else if ("/workbench/visit/pageList.do".equals(path)) {

            pageList(request, response);

        } else if ("/workbench/visit/save.do".equals(path)) {

            save(request, response);

        } else if ("/workbench/visit/edit.do".equals(path)) {

            edit(request, response);

        } else if ("/workbench/visit/deleteByIds.do".equals(path)) {

            deleteByIds(request, response);

        } else if ("/workbench/visit/update.do".equals(path)) {

            update(request, response);

        } else if ("/workbench/visit/detail.do".equals(path)) {

            detail(request, response);

        }else if ("/workbench/visit/getRemarkListByAid.do".equals(path)) {

            getRemarkListByAid(request, response);

        } else if ("/workbench/visit/deleteRemark.do".equals(path)) {

            deleteRemark(request, response);

        } else if ("/workbench/visit/saveRemark.do".equals(path)) {

            saveRemark(request, response);

        } else if ("/workbench/visit/updateRemark.do".equals(path)) {

            updateRemark(request, response);

        }


    }

    private void updateRemark(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("执行修改备注的操作");

        String id = request.getParameter("id");
        String noteContent = request.getParameter("noteContent");
        String editTime = DateTimeUtil.getSysTime();
        String editBy = ((User) request.getSession().getAttribute("user")).getName();
        String editFlag = "1";

        VisitRemark vr = new VisitRemark();

        vr.setId(id);
        vr.setNoteContent(noteContent);
        vr.setEditFlag(editFlag);
        vr.setEditBy(editBy);
        vr.setEditTime(editTime);

        VisitService vs = (VisitService) ServiceFactory.getService(new VisitServiceImpl());

        boolean flag = vs.updateRemark(vr);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", flag);
        map.put("vr", vr);

        PrintJson.printJsonObj(response, map);

    }

    private void saveRemark(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("执行添加备注操作");

        String noteContent = request.getParameter("noteContent");
        String visitId = request.getParameter("visitId");
        String id = UUIDUtil.getUUID();
        String createTime = DateTimeUtil.getSysTime();
        String createBy = ((User) request.getSession().getAttribute("user")).getName();
        String editFlag = "0";

        VisitRemark vr = new VisitRemark();
        vr.setId(id);
        vr.setNoteContent(noteContent);
        vr.setVisitId(visitId);
        vr.setCreateBy(createBy);
        vr.setCreateTime(createTime);
        vr.setEditFlag(editFlag);

        VisitService vs = (VisitService) ServiceFactory.getService(new VisitServiceImpl());

        boolean flag = vs.saveRemark(vr);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", flag);
        map.put("vr", vr);

        PrintJson.printJsonObj(response, map);


    }

    private void deleteRemark(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("删除备注操作");

        String id = request.getParameter("id");

        VisitService vs = (VisitService) ServiceFactory.getService(new VisitServiceImpl());

        boolean flag = vs.deleteRemark(id);

        PrintJson.printJsonFlag(response, flag);


    }

    private void getRemarkListByAid(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("根据id，取得备注信息列表");

        String visitId = request.getParameter("visitId");

        VisitService vs = (VisitService) ServiceFactory.getService(new VisitServiceImpl());

        List<VisitRemark> vrList = vs.getRemarkListByAid(visitId);

        PrintJson.printJsonObj(response, vrList);


    }

    private void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("跳转到详细信息页");

        String id = request.getParameter("id");

        VisitService vs = (VisitService) ServiceFactory.getService(new VisitServiceImpl());

        Visit v = vs.detail(id);
        System.out.println("v = " + v);

        ContactsService cons = (ContactsService) ServiceFactory.getService(new ContactsServiceImpl());

        String contactId = v.getContactsId();
        Map<String, Object> map = cons.getUserListAndContacts(contactId);
        Contacts con = (Contacts) map.get("con");
        System.out.println("con = " + con);

        UserService us = (UserService) ServiceFactory.getService(new UserServiceImpl());

        List<User> uList = us.getUserList();

        request.setAttribute("uList", uList);
        request.setAttribute("v", v);
        request.setAttribute("con", con);

        System.out.println("=======================================");

        request.getRequestDispatcher("/workbench/visit/detail.jsp").forward(request, response);

    }

    private void update(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("执行售后修改操作");

        String tag = request.getParameter("tag");
        System.out.println("tag = " + tag);

        String id = request.getParameter("id");
        String owner = request.getParameter("owner");
        String subject = request.getParameter("subject");
        String expiryDate = request.getParameter("expiryDate");
        String contactsId = request.getParameter("contactsId");
        String state = request.getParameter("state");
        String priority = request.getParameter("priority");
        String description = request.getParameter("description");
        String startTime = request.getParameter("startTime");
        String repeatType = request.getParameter("repeatType");
        String noticeType = request.getParameter("noticeType");

        //修改时间：当前系统时间
        String editTime = DateTimeUtil.getSysTime();
        //修改人：当前登录用户
        String editBy = ((User) request.getSession().getAttribute("user")).getName();

        Visit visit = new Visit();
        visit.setId(id);
        visit.setOwner(owner);
        visit.setSubject(subject);
        visit.setExpiryDate(expiryDate);
        visit.setContactsId(contactsId);
        visit.setState(state);
        visit.setPriority(priority);
        visit.setDescription(description);
        visit.setStartTime(startTime);
        visit.setRepeatType(repeatType);
        visit.setNoticeType(noticeType);
        visit.setEditBy(editTime);
        visit.setEditTime(editBy);

        VisitService vs = (VisitService) ServiceFactory.getService(new VisitServiceImpl());

        System.out.println("visit = " + visit);

        boolean flag = vs.update(visit);

        PrintJson.printJsonFlag(response, flag);

    }

    private void deleteByIds(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("执行售后处理的删除操作");

        String ids[] = request.getParameterValues("id");

        VisitService vs = (VisitService) ServiceFactory.getService(new VisitServiceImpl());

        boolean flag = vs.deleteByIds(ids);

        PrintJson.printJsonFlag(response, flag);


    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("进入到跳转到售后处理修改页面的操作");

        String tag = request.getParameter("tag");
        System.out.println("tag = " + tag);

        String id = request.getParameter("id");
        System.out.println("id = " + id);

        VisitService vs = (VisitService) ServiceFactory.getService(new VisitServiceImpl());

        Visit v = vs.getAllById(id);
        System.out.println("v = " + v);

        ContactsService cons = (ContactsService) ServiceFactory.getService(new ContactsServiceImpl());
        String contactId = v.getContactsId();

        Map<String, Object> map = cons.getUserListAndContacts(contactId);
        Contacts con = (Contacts) map.get("con");
        System.out.println("con = " + con);

        UserService us = (UserService) ServiceFactory.getService(new UserServiceImpl());

        List<User> uList = us.getUserList();

        for (User user : uList) {
            System.out.println("user = " + user);
        }

        request.setAttribute("uList", uList);
        request.setAttribute("v", v);
        request.setAttribute("con", con);
        if (tag != null && !"".equals(tag)) {
            request.setAttribute("tag", tag);
        }
        request.getRequestDispatcher("/workbench/visit/editTask.jsp").forward(request, response);

    }

    private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {

        System.out.println("执行添加售后的操作");

        String id = UUIDUtil.getUUID();
        String owner = request.getParameter("owner");
        String subject = request.getParameter("subject");
        String expiryDate = request.getParameter("expiryDate");
        String contactsId = request.getParameter("contactsId");
        String state = request.getParameter("state");
        String priority = request.getParameter("priority");
        String description = request.getParameter("description");
        String reminderTime = request.getParameter("reminderTime");
        String startTime = request.getParameter("startTime");
        String repeatType = request.getParameter("repeatType");
        String noticeType = request.getParameter("noticeType");

        String createTime = DateTimeUtil.getSysTime();
        String createBy = ((User) request.getSession().getAttribute("user")).getName();


        Visit v = new Visit();
        v.setId(id);
        v.setOwner(owner);
        v.setSubject(subject);
        v.setExpiryDate(expiryDate);
        v.setContactsId(contactsId);
        v.setState(state);
        v.setPriority(priority);
        v.setDescription(description);
        v.setReminderTime(reminderTime);
        v.setStartTime(startTime);
        v.setRepeatType(repeatType);
        v.setNoticeType(noticeType);
        v.setCreateTime(createTime);
        v.setCreateBy(createBy);

        VisitService vs = (VisitService) ServiceFactory.getService(new VisitServiceImpl());

        System.out.println("visit = " + v);

        boolean flag = vs.save(v);

        if (flag) {
            //如果添加交易成功，跳转到列表页
            response.sendRedirect(request.getContextPath() + "/workbench/visit/index.jsp");

        } else {
            System.out.println("程序出错!!!");
        }
    }

    private void pageList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("进入到售后处理信息列表的操作（结合条件查询+分页查询）");

        String flag = request.getParameter("flag");
        if (flag != null && !"".equals(flag) && "1".equals(flag)) {
            request.getRequestDispatcher("/workbench/visit/index.jsp").forward(request, response);
            return;
        }

        String subject = request.getParameter("subject");
        String owner = request.getParameter("owner");
        String expiryDate = request.getParameter("expiryDate");
        String contacts = request.getParameter("contacts");
        String state = request.getParameter("state");
        String priority = request.getParameter("priority");

        String pageNoStr = request.getParameter("pageNo");

        int pageNo = Integer.valueOf(pageNoStr);
        //每页展现的记录数
        String pageSizeStr = request.getParameter("pageSize");
        int pageSize = Integer.valueOf(pageSizeStr);
        //计算出略过的记录数
        int skipCount = (pageNo - 1) * pageSize;

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("subject", subject);
        map.put("owner", owner);
        map.put("expiryDate", expiryDate);
        map.put("contacts", contacts);
        map.put("state", state);
        map.put("priority", priority);

        map.put("skipCount", skipCount);
        map.put("pageSize", pageSize);

        VisitService vs = (VisitService) ServiceFactory.getService(new VisitServiceImpl());

        PaginationVO<Visit> vo = vs.pageList(map);
        PrintJson.printJsonObj(response, vo);

    }

    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("进入到跳转到售后处理添加页的操作");

        UserService us = (UserService) ServiceFactory.getService(new UserServiceImpl());

        List<User> uList = us.getUserList();

        for (User user : uList) {
            System.out.println("user = " + user);
        }
        request.setAttribute("uList", uList);
        request.getRequestDispatcher("/workbench/visit/saveTask.jsp").forward(request, response);

    }


}




































